using System;
using System.Drawing;
using System.Windows.Forms;

namespace OpenComposite.Base.Controls
{
	[ToolboxBitmap(typeof(ComboBox))]
	public class CheckedComboBox : ComboBox
	{
		#region Initialization
		public CheckedComboBox()
		{
			init();
		}
		private void init()
		{
			base.Items.Clear();
			base.MaxDropDownItems = 1;
			base.DropDownStyle = ComboBoxStyle.DropDownList;

			_listform = new CheckedListForm();
			_listform.VisibleChanged += new EventHandler(_listform_VisibleChanged);
		}
		#endregion

		#region Public Members

		#region Properties
		public new CheckedListBox.ObjectCollection Items
		{
			get { return _listform.CheckedListBox.Items; }
		}
		public CheckedListBox.CheckedIndexCollection CheckedIndices
		{
			get { return _listform.CheckedListBox.CheckedIndices; }
		}
		public CheckedListBox.CheckedItemCollection CheckedItems
		{
			get { return _listform.CheckedListBox.CheckedItems; }
		}
		public CheckedListBox.SelectedIndexCollection SelectedIndices
		{
			get { return _listform.CheckedListBox.SelectedIndices; }
		}
		public CheckedListBox.SelectedObjectCollection SelectedItems
		{
			get { return _listform.CheckedListBox.SelectedItems; }
		}
		#endregion

		#region Methods
		public void SetItemChecked(int index, bool value)
		{
			_listform.CheckedListBox.SetItemChecked(index, value);
			updateText();
		}
		public void SetItemCheckState(int index, CheckState value)
		{
			_listform.CheckedListBox.SetItemCheckState(index, value);
			updateText();
		}
		public bool GetItemChecked(int index)
		{
			return _listform.CheckedListBox.GetItemChecked(index);
		}
		public CheckState GetItemCheckState(int index)
		{
			return _listform.CheckedListBox.GetItemCheckState(index);
		}
		#endregion

		#endregion

		#region Protected Members

		#region Override
		protected override void OnDropDown(EventArgs e)
		{
			if ( this.Parent != null ) {
				_listform.StartPosition = FormStartPosition.Manual;
				_listform.Location = this.Parent.PointToScreen(new Point(this.Left, this.Bottom));
				_listform.Size = new Size(this.Width, 200);
				_listform.Show();
			} else {
				base.OnDropDown(e);
			}
		}
		#endregion

		#endregion

		#region Private Members

		#region Variables
		private CheckedListForm _listform = null;
		#endregion

		#region Methods
		private void updateText()
		{
			string txt = "";
			foreach ( object obj in _listform.CheckedListBox.CheckedItems ) {
				txt += ( txt == "" ? "" : "; " ) + obj.ToString();
			}
			if ( this.DropDownStyle != ComboBoxStyle.DropDown ) {
				base.Items.Clear();
				int index = base.Items.Add(txt);
				base.SelectedIndex = index;
				base.SelectedIndex = index;
			} else {
				this.Text = txt;
			}
		}
		#endregion

		#region Event Handlers
		private void _listform_VisibleChanged(object sender, EventArgs e)
		{
			if ( !_listform.Visible )
				updateText();
		}
		#endregion

		#endregion

		#region Internal Class: CheckedListForm , Base Class: Form
		internal class CheckedListForm : Form
		{
			#region Initialization
			public CheckedListForm()
			{
				init();
			}
			private void init()
			{
				this.FormBorderStyle = FormBorderStyle.None;
				this.ShowInTaskbar = false;
				this.TopLevel = true;

				//
				// _checkedlist
				//
				_checkedlist = new CheckedListBox();
				_checkedlist.Dock = DockStyle.Fill;
				_checkedlist.CheckOnClick = true;

				_checkedlist.LostFocus += new EventHandler(_checkedlist_LostFocus);

				this.Controls.Add(_checkedlist);
			}

			#endregion

			#region Internal Members

			#region Properties
			internal CheckedListBox CheckedListBox
			{
				get { return _checkedlist; }
			}
			#endregion

			#endregion

			#region Private Members

			#region Variables
			private CheckedListBox _checkedlist = null;
			#endregion

			#region Event Handler
			private void _checkedlist_LostFocus(object sender, EventArgs e)
			{
				this.Hide();
			}
			#endregion

			#endregion
		}
		#endregion
	}
}
